#ifndef SCANNERTHREAD_H
#define SCANNERTHREAD_H

#include <QThread>
#include <QtSerialPort/QSerialPort>
#include <QVector>
#include <debuglog.h>
#include <fcntl.h>
#include <unistd.h>
#include <QDateTime>

#define GPIO_EXPORT "/sys/class/gpio/export"
#define GPIO_PIN_VAL "22"
#define GPIO_DIR "/sys/class/gpio/gpio22/direction"
#define GPIO_DIR_VAL "out"
#define GPIO_RST_VAL "/sys/class/gpio/gpio22/value"
#define GPIO_RST_VAL_H "1"
#define GPIO_RST_VAL_L "0"

class ScannerThread : public QThread
{
    Q_OBJECT
public:
    explicit ScannerThread(QObject *parent = 0);
    void sendData(const QByteArray &data);
    void openScanner();
    void closeScanner();

protected:
    void run();
    void stop();
    void gpio_export();
    void gpio_direction();
    void gpio_writerHigh();
    void gpio_writerLow();

signals:
    void ScannerSignal(const QByteArray &data);

private:
    QSerialPort *scannerPort;
    DebugLog *debugLog;
    bool threadFlag;
    bool isOpen;
    qint64 openTime;
    long timeOut;
    int HeaderFlag;
    int ReadIndex;
    QVector<char> ComData;

public slots:
    void openSerialPort();
    void closeSerialPort();
    void writeData(const QByteArray &data);
    void readData();
    void handleError(QSerialPort::SerialPortError error);
};

#endif // SCANNERTHREAD_H
